package br.com.r1.jpastandalone.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="client")
public class Client implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Id @GeneratedValue
	private Long id;
	private String firstName;
	private String lastName;
	private int hasGoodCredit;
	
	@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinColumn(name="ph_id", referencedColumnName="id")
	private Phone phone;
	
	@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@PrimaryKeyJoinColumn
	private Address address;
	
	@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinColumn(name="ccard_id", referencedColumnName="id")
	private CreditCard creditCard;
	
	@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinColumn(name="cli_id", referencedColumnName="id")
	private Collection<Document> documents;
	
	@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@JoinTable(
			name="client_avalista",
			joinColumns={@JoinColumn(name="id_cli")},
			inverseJoinColumns={@JoinColumn(name="id_ava")}
	)
	private Collection<Avalista> avalistas;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public int getHasGoodCredit() {
		return hasGoodCredit;
	}
	public void setHasGoodCredit(int hasGoodCredit) {
		this.hasGoodCredit = hasGoodCredit;
	}	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}	
	public Phone getPhone() {
		return phone;
	}
	public void setPhone(Phone phone) {
		this.phone = phone;
	}
	public CreditCard getCreditCard() {
		return creditCard;
	}
	public void setCreditCard(CreditCard creditCard) {
		this.creditCard = creditCard;
	}
	public Collection<Document> getDocuments() {
		return documents;
	}
	public void setDocuments(Collection<Document> documents) {
		this.documents = documents;
	}
	public Collection<Avalista> getAvalistas() {
		if(avalistas == null){
			avalistas = new ArrayList<Avalista>();
		}
		return avalistas;
	}
	public void setAvalistas(Collection<Avalista> avalistas) {
		this.avalistas = avalistas;
	}
	@Override
	public String toString() {
		return "Client [id=" + id + ", firstName=" + firstName + ", lastName="
				+ lastName + ", hasGoodCredit=" + hasGoodCredit + ", address="
				+ address + ", phone=" + phone + ", creditCard=" + creditCard
				+ "]";
	}	
}